package com.book.controller;

import com.book.pojo.User;
import com.book.pojo.com.book.entity.Code;
import com.book.pojo.com.book.entity.Result;
import com.book.service.feign.BookFeign;
import com.book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author xuchangshuo
 * @date 2020/10/29 9:54
 */
@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private BookFeign bookFeign;

    @Autowired
    private UserService userService;

    //登录
    @RequestMapping("/login")
    @ResponseBody
    public Result login(@RequestBody User u) {
        boolean b = userService.login(u);
        if (b) {
            System.out.println("登录成功！");
            return new Result(Code.SUCCESS_CODE, "登录成功");
        }
        return new Result(Code.FAILED_CODE, "密码获取用户名不正确");
    }

    //注册\
    @RequestMapping("/regis")
    @ResponseBody
    public Result register(@RequestBody User user) throws Exception {
        System.out.println(user);
        boolean b = userService.add(user);
        if (b) {
            return new Result(Code.SUCCESS_CODE, "注册成功");
        }
        return new Result(Code.FAILED_CODE, "用户名已经存在");
    }

    //查询用户列表
    @RequestMapping("/find")
    @ResponseBody
    public Result findUserList() {
        List userList = userService.findUserList();

        return new Result(Code.SUCCESS_CODE, userList);
    }

    //编辑用户
    @RequestMapping("/update")
    @ResponseBody
    public Result updateUser(@RequestBody User user) {
        boolean b = userService.updateUser(user);
        if (!b) {
            return new Result(Code.FAILED_CODE, "修改失败");
        }
        return new Result(Code.SUCCESS_CODE, "修改成功");
    }

    //删除用户
    @RequestMapping("/delete")
    @ResponseBody
    public Result deleteUser(@RequestParam("userId") Integer userId) {
        boolean b = userService.delete(userId);
        if (b) {
            return new Result(Code.SUCCESS_CODE, "删除成功");
        }
        return new Result(Code.FAILED_CODE, "删除失败");
    }

    //查看所有图书信息
    @RequestMapping("/findAllBooks")
    @ResponseBody
    public Result findAllBooks() {
        Result result = bookFeign.findAll();

        return result;
    }

}
